package keeto.com.vn.bms.ui.adapter;

import java.util.ArrayList;

import keeto.com.vn.bms.model.InfoMessegerModel;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class InfoChatAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<InfoMessegerModel> arrayList;
	private LayoutInflater mInflater;
	private BMSSharedPreferences preferences;

	public InfoChatAdapter(Context context,
			ArrayList<InfoMessegerModel> arrayList) {
		this.context = context;
		this.arrayList = arrayList;
		this.mInflater = LayoutInflater.from(context);
		this.preferences = new BMSSharedPreferences(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrayList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return arrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null){
			convertView = mInflater.inflate(R.layout.item_info_chat_layout,parent,false);
			holder = new ViewHolder();
			holder.rlChatleft = (RelativeLayout)convertView.findViewById(R.id.rl_item_chat_left);
			holder.tvNameLeft = (TextView)convertView.findViewById(R.id.tv_name_chat_content_left);
			holder.tvContentleft = (TextView)convertView.findViewById(R.id.tv_chat_content_left);
			holder.tvTimeleft = (TextView)convertView.findViewById(R.id.item_chat_time_left);
			
			holder.rlChatright = (RelativeLayout)convertView.findViewById(R.id.rl_item_chat_right);
			holder.tvNameRight = (TextView)convertView.findViewById(R.id.tv_name_chat_content_right);
			holder.tvContentright = (TextView)convertView.findViewById(R.id.tv_chat_content_right);
			holder.tvTimeright = (TextView)convertView.findViewById(R.id.item_chat_time_right);
			holder.rlChatleft.setVisibility(View.GONE);
			holder.rlChatright.setVisibility(View.GONE);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		InfoMessegerModel messegerModel = arrayList.get(position);
		if (messegerModel.getUserID().equals(preferences.getUserID(context))){
			holder.rlChatleft.setVisibility(View.GONE);
			holder.rlChatright.setVisibility(View.VISIBLE);
			holder.tvNameRight.setText(messegerModel.getFullName());
			holder.tvContentright.setText(Html.fromHtml(messegerModel.getMsgContent()));
			holder.tvTimeright.setText(Utils.convertTime(messegerModel.getMsgTime()));
		}else{
			holder.rlChatleft.setVisibility(View.VISIBLE);
			holder.rlChatright.setVisibility(View.GONE);
			holder.tvNameLeft.setText(messegerModel.getFullName());
			holder.tvContentleft.setText(Html.fromHtml(messegerModel.getMsgContent()));
			holder.tvTimeleft.setText(Utils.convertTime(messegerModel.getMsgTime()));
		}

		return convertView;
	}

	class ViewHolder{
		private RelativeLayout rlChatleft;
		private TextView tvNameLeft;
		private	TextView tvContentleft;
		private TextView tvTimeleft;
		private RelativeLayout rlChatright;
		private TextView tvNameRight;
		private TextView tvContentright;
		private TextView tvTimeright;
	}

}
